package com.salon.modules.sys.controller;

import com.salon.common.annotation.SysLog;
import com.salon.common.http.response.ObjectResult;
import com.salon.common.http.response.PageResult;
import com.salon.common.utils.Constant;
import com.salon.common.validator.ValidatorUtils;
import com.salon.modules.sys.entity.SysRoleEntity;
import com.salon.modules.sys.service.ISysRoleMenuService;
import com.salon.modules.sys.service.ISysRoleService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 角色管理
 */
@RestController
@RequestMapping("/sys/role")
@Api(tags="角色管理")
public class SysRoleController extends BaseController {

    @Resource
    private ISysRoleService sysRoleService;

    @Resource
    private ISysRoleMenuService sysRoleMenuService;

    @GetMapping("/list")
    @ApiOperation("分页")
    @RequiresPermissions("sys:role:select")
    public PageResult<SysRoleEntity> list(@ApiIgnore @RequestParam Map<String, Object> params) {
        // 如果不是超级管理员，则只查询自己创建的角色列表
        if (getUserId() != Constant.SUPER_ADMIN) {
            params.put("createUserId", getUserId());
        }
        return sysRoleService.listByPage(params);
    }

    @GetMapping("/select")
    @ApiOperation("查询全部")
    @RequiresPermissions("sys:role:select")
    public ObjectResult<List<SysRoleEntity>> select() {
        Map<String, Object> map = new HashMap<>();
        // 如果不是超级管理员，则只查询自己所拥有的角色列表
        if (getUserId() != Constant.SUPER_ADMIN) {
            map.put("create_user_id", getUserId());
        }
        List<SysRoleEntity> list = sysRoleService.listByMap(map);
        return ObjectResult.success(list);
    }

    @GetMapping("/info/{roleId}")
    @ApiOperation("详情")
    @RequiresPermissions("sys:role:select")
    public ObjectResult<SysRoleEntity> info(@PathVariable("roleId") Long roleId) {
        SysRoleEntity role = sysRoleService.getById(roleId);
        // 查询角色对应的菜单
        List<Long> menuIdList = sysRoleMenuService.listMenuIdsByRoleId(roleId);
        role.setMenuIdList(menuIdList);
        return ObjectResult.success(role);
    }

    @SysLog("保存角色")
    @ApiOperation("保存")
    @PostMapping("/save")
    @RequiresPermissions("sys:role:save")
    public ObjectResult<String> save(@RequestBody SysRoleEntity role) {
        ValidatorUtils.validateEntity(role);
        role.setCreateUserId(getUserId());
        sysRoleService.saveRole(role);
        return ObjectResult.success();
    }

    @SysLog("修改角色")
    @ApiOperation("修改")
    @PostMapping("/update")
    @RequiresPermissions("sys:role:update")
    public ObjectResult<String> update(@RequestBody SysRoleEntity role) {
        ValidatorUtils.validateEntity(role);
        role.setCreateUserId(getUserId());
        sysRoleService.update(role);
        return ObjectResult.success();
    }

    @SysLog("删除角色")
    @PostMapping("/delete")
    @ApiOperation("删除")
    @RequiresPermissions("sys:role:delete")
    public ObjectResult<String> delete(@RequestBody Long[] roleIds) {
        sysRoleService.deleteBatch(roleIds);
        return ObjectResult.success();
    }

}
